<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Favorite_Product_Model extends CI_Model {

    protected $_lang = '';
    protected $_table = 'aw_favorite_product';

    function __construct() {
        parent::__construct();
    }

    public function initialise($lang) {
        if ($lang != '' && $lang != 'vn')
            $this->_lang = '_' . $lang;
        else
            $this->_lang = '';
    }

    function getFavorite($product_id, $user_id) {
        $this->db->where('product_id', $product_id);
        $this->db->where('user_id', $user_id);
        $result = $this->db->get($this->_table)->result();
        if (!empty($result)) {
            return current($result);
        }
        return false;
    }
    
    function getProducts($user_id) {
        $this->db->select('aw_products.*');
        $this->db->join($this->_table, "aw_products.products_id = {$this->_table}.product_id AND {$this->_table}.user_id = {$user_id}");
        $result = $this->db->get('aw_products')->result();
        if(!empty($result)) {
            foreach($result as $key=>$item) {
                $salesOff = 0;
                if($item->sprice>0&&$item->price>0)
                    $salesOff = 100-round($item->sprice/$item->price*100);
                $result[$key]->salesOff = $salesOff;
            }
        }
        return $result;
    }

    function delete($favorite_product_id) {
        $this->db->where('favorite_product_id', $favorite_product_id);
        $query = $this->db->delete($this->_table);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }

    function saveData($edit_id, $data) {
        if ($edit_id != 0) {
            $this->db->where('favorite_product_id', $edit_id);
            if ($this->db->update($this->_table, $data)) {
                return true;
            } else {
                return false;
            }
        } else {
            if ($this->db->insert($this->_table, $data)) {
                return true;
            } else {
                return false;
            }
        }
    }

}